﻿package 
{
	import caurina.transitions.Tweener;
	import com.sonishpere.BlackHoleManager;
	import com.sonishpere.Content;
	import com.saint.utils.Logger;
	import com.sonishpere.data.Countrys;
	import com.sonishpere.events.BlackHoleEvent;
	import com.sonishpere.events.CountryButtonClickEvent;
	import com.sonishpere.IApplication;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.DisplacementMapFilter;
	import flash.filters.DisplacementMapFilterMode;
	import flash.geom.Point;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author kembera
	 */
	public class Application extends Sprite implements IApplication
	{
		
		private var background:MovieClip;
		private var content:Content;
		private var bgTile:BitmapData;
		var timer:Timer;
		
		
		/* BLACK HOLE VARIABLES --------------------*/
		
		private var bgHolder:Sprite = new Sprite();
				
		/* start point of the black hole void */
		private var pntXStart:Number = 10;
		private var pntYStart:Number = 150;
				
		/* holds the current position of the black hole */
		private var pntX:Number;
		public var pntY:Number;
		
		/* bounds of the xMovement */
		private var blackHoleXMin: Number;		
		private var blackHoleXMax: Number;
		
		private var blackHoldXBorderLeft:Number = 50;
		private var blackHoldXBorderRight:Number = 380;		
		
		private var bmpToDistort:Bitmap;
		
		private var blackHoleCentre:Sprite = new Sprite();
		var blackHoleSuckingManager:BlackHoleManager;
		
		private var xMov:Number = 0.5;
		private var yMov:Number = 1;
		private var yMovMax:Number = 20;
		
		private var dissapearingPoint:Point;
		
		private var _country:String;
		
		/*----------------------------------------------*/
		
		var initialResize:Boolean = false;
		
		
		/**
		 * CONSTRUCTOR
		 */
		public function Application() 
		{
			Logger.info("Application.Application");
			
			//init();
		}
			
		/**
		 * INIT
		 */
		public function init():void
		{	
	
			stage.addEventListener(Event.RESIZE, resizeHandler);
			
		
			addChild(bgHolder);
			
			/*add the black hole centre */
			blackHoleCentre = new BlackHoleCentre();
			addChild(blackHoleCentre);	
			
			/* attach the content MC - from the library initially */
			content = new Content();
			addChild(content);
			content.init();
			content.x = stage.stageWidth / 2 - (content.width/2);
			content.y = stage.stageHeight / 2 - (content.height / 2);
			
			
			/* create black hole manager to make the content dissapear 
			  */
			blackHoleSuckingManager = new BlackHoleManager(content, stage);	
			blackHoleSuckingManager.addEventListener(BlackHoleEvent.COLLAPSE_COMPLETE, onCollapseComplete)
			
			bgTile = new SpaceTile(0, 0);
					
			/* get the source image to distort */			
			var bmpDataToDistort:BitmapData = new Nebula(0, 0);
			bmpToDistort = new Bitmap(bmpDataToDistort);
			bgHolder.addChild(bmpToDistort)		
			
			/* set the start point of the blackHole*/
			pntX  = pntXStart;
			pntY  = pntYStart;
				
								
			
			//init();			
		
			
		
			resizeHandler(null);
			addEventListener(Event.ENTER_FRAME, enterFrameFunc);	
			tweenUp();			
		}
		
		
	
		
		
		
		
		
		/**
		 * resize handler
		 * @param	event
		 */
		function resizeHandler(event:Event):void 
		{
			Logger.info("Application.resizeHandler");
			
			/* keep background anchored to top left */
			//background.x = 0;
			//background.y = 0;
			
			/* tiles the space background */
			//graphics.clear();
			//graphics.beginBitmapFill(bgTile);
			//graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			//graphics.endFill();
			
			
			bgHolder.x =  0//stage.stageWidth / 2 - (bgHolder.width/2);
			bgHolder.y = 0//stage.stageHeight / 2 - (bgHolder.height/2);
			bgHolder.width = stage.stageWidth;
			bgHolder.height = stage.stageHeight;
			
			/* centre the content */
			content.x = stage.stageWidth / 2 - (content.width/2) + 100;
			content.y = stage.stageHeight / 2 - (content.height / 2);
			
			/* keep the black hole 'anim' within the screen bounds*/
			
			trace("");
			blackHoleXMax = bgHolder.width - blackHoldXBorderRight//+ bgHolder.width - blackHoldXBorder;
			blackHoleXMin = blackHoldXBorderLeft;
		}
		
		
		/**
		 * ENTER FRAME FUNCTIONS
		 * 
		 * BLACK HOLE FUNCTIONS
		 * 
		 * @param	e
		 */ 
		private function enterFrameFunc(e:Event):void 
		{
			
			pntX += xMov;	
			
			/* keep the black hole within the borders */
			if (pntX > blackHoleXMax || pntX > stage.stageWidth){
				xMov *= -1;
				pntX += xMov;
			}			
			
			if (pntX < blackHoleXMin  )			{
				pntX = blackHoleXMin 
				xMov *= -1;
			}
			
			//if (pntY >= stage.stageHeight - 500 || pntY < 10) yMov *= -1;			
			
			var xOffset:Number = 200;
			var yOffset:Number = 200;
			
			dissapearingPoint= new Point();
			dissapearingPoint.x = pntX + xOffset + bgHolder.x;
			dissapearingPoint.y = pntY + yOffset + bgHolder.y;
			
			blackHoleSuckingManager.dissapearingPoint = dissapearingPoint;
			
            var myFilters:Array = createFilter();
			bmpToDistort.filters = myFilters;
			blackHoleCentre.x = pntX + xOffset;
			blackHoleCentre.y = pntY + yOffset;
				
		}
		
		
		
		/**
		 * TWEENS THE BLACK HOLE
		 */
		private function tweenUp():void
		{
			Tweener.addTween(this, { pntY: pntYStart - yMovMax, time:9, onComplete:tweenDown, transition:"easeInOutCubic" } );

		}
		private function tweenDown():void
		{
			Tweener.addTween(this, { pntY: pntYStart + yMovMax, time:9, onComplete:tweenUp, transition:"easeInOutCubic" } );

		}
		
		
		
        private function createFilter():Array {
            var filter:BitmapFilter = getBitmapFilter();
            return new Array(filter);
        }
		
        private function getBitmapFilter():BitmapFilter {
            var mapBitmap:BitmapData = createBitmapData();
			
            var mapPoint:Point       = new Point(pntX, pntY);
            var channelRed:uint      = BitmapDataChannel.RED;
            var channelGreen:uint    = BitmapDataChannel.GREEN;
            var channelBlue:uint     = BitmapDataChannel.BLUE;
            var componentX:uint      = channelBlue;
            var componentY:uint      = channelRed;
            var scaleX:Number        = 150;
            var scaleY:Number        = 150;
            var mode:String          = DisplacementMapFilterMode.CLAMP;
            var color:uint           = 0;
            var alpha:Number         = 0;
            return new DisplacementMapFilter(mapBitmap,
                                             mapPoint,
                                             componentX,
                                             componentY,
                                             scaleX,
                                             scaleY,
                                             mode,
                                             color,
                                             alpha);
        }
		
		
		
        private function createBitmapData():BitmapData {
			var filterMC:MovieClip = new Filter();
			
			
			/* create the 'neutral' bitmap to start with  
			 * 
			 * '80' is the neutral value for each channel
			 * 
			 * */
            var bitmapData:BitmapData = new BitmapData(filterMC.width, filterMC.height, true, 0x80808080 );
            bitmapData.draw(filterMC);
            //var bitmap:Bitmap = new Bitmap(bitmapData);
			
           
			//addChild(bitmap);
            return bitmapData;
        }
		
		
		
		
		
		/**
		 * NAVIGATING TO URL AFTER THE BLACK HOLE COLLAPSE
		 */
		private function onCollapseComplete(e:BlackHoleEvent):void 
		{
			Logger.info("Application.onCollapseComplete : e.country" + e.country);
			_country = e.country;
			
			var url :String;
			
			switch(e.country)
			{
				case Countrys.BULGARIA:
					url = "http://bg.sonispherefestivals.com";						
				break;
				case Countrys.CZECH_REPUBLIC:
					url = "http://cz.sonispherefestivals.com";						
				break;
				case Countrys.FINLAND:
					url = "http://fi.sonispherefestivals.com";						
				break;
				case Countrys.GREECE:
					url = "http://gr.sonispherefestivals.com";						
				break;
				case Countrys.POLAND:
					url = "http://pl.sonispherefestivals.com";									
				break;
				case Countrys.ROMANIA:
					url = "http://rm.sonispherefestivals.com";						
				break;
				case Countrys.SPAIN:
					url = "http://es.sonispherefestivals.com";						
				break;
				case Countrys.SWEDEN:
					url = "http://se.sonispherefestivals.com";						
				break;
				case Countrys.SWITZERLAND:
					url = "http://sz.sonispherefestivals.com";						
				break;
				case Countrys.TURKEY:
					url = "http://tk.sonispherefestivals.com";						
				break;
				case Countrys.UNITED_KINGDOM:
					url = "http://uk.sonispherefestivals.com";						
				break;
				
				
				default:
					throw new Error("Application onCollapseComplete - unknown country: " + e.country);
				break;
			}
			
			var request:URLRequest = new URLRequest(url);
			try {
			  navigateToURL(request, '_self'); // second argument is target
			} catch (e:Error) {
			  trace("Error occurred!");
			}
	
		}
		
		
		
		
	}
	
}